/**
 * (C) copyright geolabs 2007
 * author: Gérald Fenoy gerald@cartography.st
 *
 */
System.require("OctoXml");

/*
 * Imagemap Controler class
 */
Imagemap=Controler.extend();
Imagemap.define({
  _init: function(){
      this.element=document.getElementById(arguments[0]);
      this.width=375+25;
      this.height=30;
      this.info=document.getElementById('infobulle');
      this.info_comm=document.getElementById('commune');
      this.info_val=document.getElementById('valeur');
      this.container=document.getElementById('carteWinC');
      this.window=carte.window;
      this.elements={};
      this.container.onmousemove=this.registerMousePosition.bind(this);
    },

  registerMousePosition: function(){
      this.mousePos=Position.fromEvent(arguments[0]);
      this.scroll=Position.getScroll(this.container);
	this.mousePos.x+=10;
	this.mousePos.y+=10;
      //this.mousePos.x+=(this.scroll.x?(this.scroll.x-10):10);
      //this.mousePos.y+=(this.scroll.y?(this.scroll.y-10):10);
      //document.body.appendChild(document.createTextNode('Mouse pos : ('+this.mousePos.x+","+this.mousePos.y+")"));
    },

  hideData: function(e){
      this.info.style.display='none';
    },

  displayData: function(){
      var tmp=this.window.getSize();
      //alert("Inside CarteWinC = ("+tmp[0]+","+tmp[1]+")");
      this.info.style.top=(((this.mousePos.y+this.height/*-(this.scroll.y?this.scroll.y:0)*/)<tmp[1])?this.mousePos.y:this.mousePos.y-(this.height*2))+'px';
      this.info.style.left=(((this.mousePos.x+this.width/*-(this.scroll.x?this.scroll.x:0)*/)<tmp[0])?this.mousePos.x:this.mousePos.x-(this.width-20))+'px';
      this.info.style.display='block';
      var nom="";
      var lastIsSpace=false;
      for(var t in arguments[1])
	if(t==0)
	  nom+=arguments[1][t].toUpperCase();
	else{
	  if(!lastIsSpace)
	    nom+=arguments[1][t];
	  else
	    nom+=arguments[1][t].toUpperCase();
	  if(arguments[1][t]=="-" || arguments[1][t]==" " )
	    lastIsSpace=true;
	  else
	    lastIsSpace=false;
	}
      this.info_comm.innerHTML=(nom?nom:arguments[1]);//+" ("+arguments[2]+")";
      this.info_val.innerHTML="";
      if(this.elements[arguments[2]]){
	this.info_val.innerHTML=" : "+this.elements[arguments[2]];
      }
    },

  newData: function(){
      //alert("toto");
      var toto=OctoXml.jsonFromElement(this.request.responseXML.getElementsByTagName('result')[0])['result'];
      //alert(toto);
      this.request=null;

      /**
       * Property elements is used here as an object rather than an array of
       * array because of that using an object imply that access method are in
       * o(1) rather than o(n).
       */
      this.elements={};
      for(var i=0;i<toto['id'].length;i++)
	this.elements[toto['id'][i]]=toto['value'][i];

      //document.body.innerHTML+=OctoXml.dumpAsJson(this.elements);
    }
  });
